<?php

namespace App\Http\Controllers;

use Alipay\OpenAPISDK\Api\AlipayTradeApi;
use Alipay\OpenAPISDK\ApiException;
use Alipay\OpenAPISDK\Model\AlipayTradePayModel;
use GuzzleHttp\Client;
use Alipay\OpenAPISDK\Util\Model\AlipayConfig;

use Alipay\OpenAPISDK\Util\AlipayConfigUtil;
use Alipay\OpenAPISDK\Util\GenericExecuteApi;

use Alipay\OpenAPISDK\Util\Model\CustomizedParams;
use Alipay\OpenAPISDK\Util\Model\OpenApiGenericRequest;
use Illuminate\Http\Request;


class Pay3Controller
{
    private $appid = '2021004170645344';
    protected $privateKey = 'MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCwA6+7LuBROUMF1MK10mrv8H1xK/9zB6PUjF+3mvaTCE4+OLZSIU72UuGoXkeZIQE9VEwqPFqTAsvWBuBOGim26frlBJxh0zUX+hwyFGxPiM55jvI+YCWo3wEfOT9QUk8Lw3U0H8C/VLl6xd7hbLb0ajbjDfPQEP86yCigngZfI+E0WpOGiVcl1Qp+vXqzWJi0iGFI2W7dBLbik+mRE1wKOjwU3KD1EdyWbLz8Eu69q4/5ReiYKi+nZTPWRSdxqwdsY1EoJECaj2M38O7iaKGhpf9SkeT7A+6n61C8DfBOZSV2ipkVp6azAAu6+SkJIFsDxuRzi1YZ3HZpq7nVlhdBAgMBAAECggEALfaoez8AgPVtFSa8OfLazhGFEHqm2IkUNTr6+91Rqo+urvzsT4I3NofqOad1Fv3RbwyGXzknheBjDl75UCvw3L05MXkExV78LGi52DIRHqvhD0lO5Q8ChpjFmDvA1t7HmZDPGG96S/x5+x+U1lUnaaga8SwBkUH/BICka10Y2Nr8yDAjfkItRiw0PPcBRwOOKkNXLtDXrqVFjMgb0vpna3DovyVkfW4FaADoA4M63KpPvxF+BfYDrN9BmiKoBrIjWrVCAArgG/2W8lBoAZKuP0IdG6YFfXLE475hltDmPDk31rXkVm3SXSqSiOAIzx8QROHMgFU+aHcYIN/VuvucaQKBgQDe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FGbC7cgD89VmHjSl/6WM8gA3OihLjxl0jFME9RQJ016/DSA+S0GIjbC/KdIzi1yHAvWAt56+gPROLi/YQxXqXafwFf96e/w99w6rrkE/Osiw3NHxImTa1lLLg+GQr2oLKhw08USKdMdJLqxvJzDZ8KPwtv4ayn5IjQ1+7AWvzHMegz2oiSr0WYy9Q4KLxMC+KHQIDAQAB';

    public function pc(Request $request)
    {
        $params = $request->all();
        $amount = $params['amout'];
        // 初始化SDK
        $alipayConfigUtil = new AlipayConfigUtil($this->getAlipayConfig());
        $outTradeNo = build_uri();
        $apiInstance = new GenericExecuteApi(
            $alipayConfigUtil,
            new Client()
        );
        // 构造请求参数以调用接口
        $bizParams = array();
        $bizContent = array();
        // 设置订单绝对超时时间
        $bizContent['time_expire'] = date('Y-m-d H:i:s', time() + 30 * 60);
        $bizContent['subject'] = "渠道商进件充值";
        // 设置产品码
        $bizContent['product_code'] = "FAST_INSTANT_TRADE_PAY";
        $count = 1;
        // 设置订单附加信息
        $bizContent['body'] = "充值进件量" . $count.'个';
        // 设置PC扫码支付的方式
        //        $bizContent['qr_pay_mode'] = "1";// 设置请求后页面的集成方式
        $bizContent['integration_type'] = "PCWEB";


        // 设置建议使用time_expire字段
//        $bizContent['timeout_express'] = "90m";


        // 设置请求来源地址
        //$bizContent['request_from_url'] = "https://";

        // 设置商户自定义二维码宽度
        $bizContent['qrcode_width'] = 100;


        // 设置商户订单号
        $bizContent['out_trade_no'] = $outTradeNo;

        // 设置订单总金额
        $bizContent['total_amount'] = bcadd($amount, 0, 2);


        $bizParams['biz_content'] = $bizContent;

        try {
            // 如果是第三方代调用模式，请设置app_auth_token（应用授权令牌）
            $pageRedirectionData = $apiInstance->pageExecute('alipay.trade.page.pay', 'POST', $bizParams);
            // 如果需要返回GET请求，请使用
//             $pageRedirectionData = $apiInstance->pageExecute('alipay.trade.page.pay', 'GET', $bizParams);
            print_r($pageRedirectionData);
            $dir = 'storage/pay_html/' . date('Ymd');
            if (!is_dir($dir)) {
                mkdir($dir, 0777, true);
            }
            $path = $dir . '/1' . '.html';
            file_put_contents($path, $pageRedirectionData);
        } catch (ApiException $e) {
            echo '调用失败: ', $e->getMessage(), PHP_EOL;
            echo 'body: ', $e->getResponseBody(), PHP_EOL;
            echo 'header: ', $e->getResponseHeaders(), PHP_EOL;
        }


    }

    public function getAlipayConfig()
    {
        $alipayConfig = new AlipayConfig();
        $alipayConfig->setServerUrl('https://openapi.alipay.com');
        $alipayConfig->setAppId($this->appid);
        $alipayConfig->setPrivateKey($this->privateKey);
        $alipayConfig->setAlipayPublicKey($this->publicKey);
        return $alipayConfig;
    }
}
